﻿using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Collections.Generic;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    XmlDataDocument xmldoc = new XmlDataDocument();
    XmlNodeList xmlnode;
    //FileStream fs = new FileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "unit.xml"), FileMode.Open, FileAccess.Read);
    string fs;
    public Service()
    {
        
    }
    private List<string> ListUnit = new List<string>();
    private List<string> ListChildUnitName = new List<string>();
    private List<string> ListChildUnit = new List<string>();
    private List<string> ListRateName = new List<string>();
    private List<string> ListRate = new List<string>();
    private void getUnit()
    {
        fs = Server.MapPath("unit.xml");
        xmldoc.Load(fs);
        xmlnode = xmldoc.GetElementsByTagName("Unit");
        for (int i = 0; i < xmlnode.Count; i++)
        {
            ListUnit.Add(xmlnode[i].ChildNodes.Item(1).InnerText.Trim());
        }
    }

    [WebMethod]
    public int getCount() {
        getUnit();
        return xmlnode.Count; 
    }

    [WebMethod]
    public string getUnitId(int i)
    {
        getUnit();
        return xmlnode[i].ChildNodes.Item(0).InnerText.Trim();
    }

    [WebMethod]
    public List<string> getListUnit()
    {
        getUnit();
        return ListUnit;
    }

    [WebMethod]
    public List<string> getListChildUnitName(int temp)
    {
        getUnit();
        for (int i = 2; i < xmlnode[temp].ChildNodes.Count; i++)
        {
            ListChildUnitName.Add(xmlnode[temp].ChildNodes.Item(i).Name);
        }
        return ListChildUnitName;
    }

    [WebMethod]
    public List<string> getListChildUnit(int temp)
    {
        getUnit();
        for (int i = 2; i < xmlnode[temp].ChildNodes.Count; i++)
        {
            ListChildUnit.Add(xmlnode[temp].ChildNodes.Item(i).ChildNodes.Item(0).InnerText.Trim());
        }
        return ListChildUnit;
    }

    [WebMethod]
    public List<string> getListRateName(int temp,int temp2)
    {
        getUnit();
        temp2 += 2;
        for (int i = 0; i < xmlnode[temp].ChildNodes.Item(temp2).ChildNodes.Item(1).ChildNodes.Count; i++)
        {
            ListRateName.Add(xmlnode[temp].ChildNodes.Item(temp2).ChildNodes.Item(1).ChildNodes.Item(i).Name);
        }
        return ListRateName;
    }

    [WebMethod]
    public List<string> getListRate(int temp, int temp2)
    {
        getUnit();
        temp2 += 2;
        for (int i = 0; i < xmlnode[temp].ChildNodes.Item(temp2).ChildNodes.Item(1).ChildNodes.Count; i++)
        {
            ListRate.Add(xmlnode[temp].ChildNodes.Item(temp2).ChildNodes.Item(1).ChildNodes.Item(i).InnerText.Trim());
        }
        return ListRate;
    }


}
